/**
 *
 * @file-name: cart.js
 *
 *	@auther: wcc
 *
 *	@info: model cart
 *
 **/

var HOME_URL = '/EachGo/index.php/Home';

//这个是购物车的收藏夹

$(function() {
    $(".put-in-collect").click(function(e) {
        var $this = $(e.target);
        var arr = {};
        arr['goods_ID'] = $this.closest('.cart').find('.goods-id').val();
        arr['isBingo'] = $this.closest('.cart').find('.isbingo').val();
        $.get(HOME_URL+'/Collect/addCollect',
            arr,
            function(data){
                flyStar();
                console.log(data);
            },"json");
    });
});

$(document).ready( function() {

    //    操作按钮时执行
    $('.operate').bind("click", function() {
        var operate = $(this).text();
        var parent = $(this).parent().parent().parent();
        var price = parent.find('.price');
        var amount = parent.find(".amount");
        var total = parent.find('.showtotal');

        if(operate == "+") {
            amount.attr('value', parseInt(amount.val()) + 1);
        } else if(operate == '—') {
            if(parseInt(amount.val()) > 1) {
                amount.attr('value', parseInt(amount.val()) - 1);
            }
        }
        total.html( (parseInt(amount.val()) * parseFloat(price.val()) ).toFixed(2) );
        count();
    });
    /***  计算总计  ***/
    function count() {
        var allamountN = 0;
        var alltotalN = 0;
        $('.amount').each(function() {
            allamountN = parseInt(allamountN) + parseInt( $(this).val() );
        });
        $('#allamount').text(allamountN);


        $('.showtotal').each(function() {
            alltotalN = parseFloat(alltotalN) + parseFloat($(this).text());
        });
        $('#alltotal').text(alltotalN.toFixed(2));
    }

    /*
       单个删除按钮点击
     */
    $('.delete').click(function() {
        var obj = $(this).closest('tr');//.remove();
        var id = obj.find(".cart-id").val();
        $.get(HOME_URL+'/ShoppingCart/delCart',
            {id:id},
            function(data){
                obj.remove();
                count();
                console.log(data);
            },"json");
    });

    /**       删除全部所选      **/
    $('.delete-all').bind("click",function() {
        var $objs = $('.cart-id:checked');
        var arr = {};
        for( var i=0; i<$objs.length;i++){
            arr[i] = $objs.get(i).value;
            $objs.get(i).closest('tr').remove();
        }
        $.get(HOME_URL+'/ShoppingCart/delCart',
            arr,
            function(data) {
                count();
                console.log(data);
            },"json");
    });


    /**  页面准备好时就执行总计   **/
    $(function() {
        count();
    });
});



$(function(){/*全选&取消全选*/
    $(".all").click(function(){/*prop()对于需要使用一个函数设置多个属性值或是一次性设置多个属性值的情况    1属性值和属性名相同时     2属性值只能为true和false的时候*/
        if($(this).is(":checked")) {
            $(".cart-id").prop("checked",true);
            $(".all").prop("checked",true);
        }
        else if($(this).not(":checked")){
            $(".cart-id").prop("checked",false);
            $(".all").prop("checked",false);
        }
    });
});


/**
 *
 * @author Mitol
 *
 * @info shopcart fly star
 *
 */
function flyStar() {
    var offset_end = $("#end").offset();
    $(".addcar").click(function(event){
        var addcar = $(this);
        var offset_start = $(this).offset();
        var img = addcar.parent().find('img').attr('src');
        var flyer = $('<img class="u-flyer" src="'+img+'" style="display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;">');

        if( offset_start.top > $(window).height()) {
            offset_start.top -= $(window).height();
        }


        flyer.fly({
            start: {
                left: offset_start.left, //飞星起点#fly 距离左边的 position: fixed
                top: offset_start.top    //飞星起点 距离右边的
            },
            end: {
                left: offset_end.left-195,  //终点距离浏览器顶的距离
                top: 10,    //终点距离浏览器顶的距离
                width: 20, //飞星消失时候的宽度
                height: 20 //飞星消失时候的高度
            },
            onEnd: function(){ //飞星结束后执行
                //alert(offset_start.top + ","+ $(window).height());
                //$("#msg").show().animate({width: '250px'}, 200).fadeOut(1000);
                addcar.css({"cursor":"default","background-color":"#CCC"}).unbind('click');
                addcar.text("已加入购物车");
                this.destory();
            }
        });
    });
}
$(function() {
    flyStar();
});